<template>
  <div id="TransactionRecord">
    <div class="query">
      <!-- --------------- -->
      <div class="homeTitle admin">
        <span>交易申请记录查询</span>
        <el-input placeholder="搜索单据编号" v-model="search" class="input-with-select">
          <el-button slot="append" icon="el-icon-search" @click="getdata(1)"></el-button>
        </el-input>
      </div>
      <div class="queryCon">
        <div class="qSeles">
          <!-- 两个选择 -->
          <div class="xuanze">
            <div class="xuanzeCon">
              <span>待办类型：</span>
              <el-select v-model="dblx" placeholder="请选择" size="small">
                <el-option
                  v-for="item in dqztOption"
                  :key="item.label"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </div>
            <div class="xuanzeCon">
              <span>选择行社\机构：</span>
              <!-- <el-input v-model="banks" placeholder="点击进行多选" size="mini"></el-input> -->
              <el-tooltip :content="banks? banks: '未选择任何行社/机构'" placement="top">
                <el-button size="small" @click="morechoose">点击进行多选</el-button>
              </el-tooltip>
            </div>
            <div class="xuanzeCon">
              <span>当前状态：</span>
              <el-select v-model="dqzt" placeholder="请选择" size="small">
                <el-option v-for="item in dblxOption" :key="item" :label="item" :value="item"></el-option>
              </el-select>
            </div>
          </div>
          <!-- 按钮 -->
          <div class="btnAb">
            <el-button type="primary" size="small" @click="getdata(1)">查询</el-button>
            <el-button type="primary" size="small" @click="daochu">导出</el-button>
          </div>
        </div>
      </div>
    </div>
    <!-- 表格 -->
    <div class="table">
      <!-- 表格 -->
      <div class="tablecon">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column prop="no" label="单据编号" align="center"></el-table-column>
          <el-table-column prop="type" label="待办类型" align="center"></el-table-column>
          <el-table-column prop="bank" label="行社\机构" align="center"></el-table-column>
          <el-table-column prop="money" label="交易金额(万元)" align="center"></el-table-column>
          <el-table-column prop="tradeDirection" label="交易方向" align="center"></el-table-column>
          <el-table-column prop="state" label="当前状态" align="center"></el-table-column>
          <el-table-column prop="address" label="操作" align="center" width="172">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="lookDetail(scope.row)">查看</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="pgfy">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="10"
          layout="total, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "TransactionRecord",
  data() {
    return {
      dqztOption: [
        { value: "", label: "全部" },
        { value: "债券交易", label: "债券交易" },
        { value: "债券回购交易", label: "债券回购交易" },
        { value: "同业拆借交易", label: "同业拆借交易" },
        { value: "同业存单交易", label: "同业存单交易" },
        { value: "存放同业交易", label: "存放同业交易" },
        { value: "债券借贷交易", label: "债券借贷交易" },
        { value: "理财产品交易", label: "理财产品交易" }
      ],
      dqzt: "全部",
      dblxOption: [
        "全部",
        "申请待复核",
        "部门经理审核中",
        "分管领导审核中",
        "行长审核中",
        "理事长审核中",
        "授权资料待提交",
        "省联社授权中",
        "申请审核通过",
        "申请被退回",
        "申请已撤销"
      ],
      dblx: "",
      banks: "",
      banksArr: [],
      banksId: "",
      value: "",
      search: "",
      tableData: [],
      banksObj: [],
      // 分页
      currentPage: 1,
      total: null,
      obj: ""
    };
  },
  beforeDestroy: function() {
    this.$bus.$emit("chooseBank");
  },
  methods: {
    daochu() {
      let obj = {
        no: this.search,
        // 逗号拼隔
        bankList: this.banksId,
        type: this.dblx,
        state: this.dqzt
      };
      this.axios
        .get("applyRecord/exportApplyRecord", {
          params: this.filterNoneObj(obj),
          responseType: "blob"
        })
        .then(res => {
          this.download(res.data, "交易申请记录");
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    download(data, filename) {
      if (!data) {
        return;
      }
      var blob = new Blob([data], { type: "application/vnd.ms-excel" });
      var url = window.URL.createObjectURL(blob);
      var aLink = document.createElement("a");
      aLink.style.display = "none";
      aLink.href = url;
      aLink.setAttribute("download", decodeURI(filename));
      document.body.appendChild(aLink);
      aLink.click();
      document.body.removeChild(aLink); //下载完成移除元素
      window.URL.revokeObjectURL(url); //释放掉blob对象
    },
    getdata(val) {
      this.currentPage = val;
      let obj = {
        no: this.search,
        // 逗号拼隔
        bankList: this.banksId,
        type: this.dblx,
        state: this.dqzt,
        page: this.currentPage
      };
      this.obj = obj;
      this.axios
        .get("applyRecord/selectList", { params: this.filterNoneObj(obj) })
        .then(({ data }) => {
          this.tableData = data.data;
          this.total = data.pageInfo.total;
        });
    },
    paramsGetdata() {
      let obj = JSON.parse(sessionStorage.getItem("TransactionRecordParams"));
      this.obj = obj;
      this.search = obj.no;
      this.banksId = obj.bankList;
      this.dblx = obj.type;
      this.dqzt = obj.state;
      this.currentPage = obj.page;
      this.banks = obj.banks;
      this.axios
        .get("applyRecord/selectList", { params: this.filterNoneObj(obj) })
        .then(({ data }) => {
          this.tableData = data.data;
          this.total = data.pageInfo.total;
        });
    },
    lookDetail(row) {
      let obj = this.obj;
      obj.banks = this.banks;
      sessionStorage.setItem(
        "TransactionRecordParams",
        JSON.stringify(this.obj)
      );
      switch (row.type) {
        case "理财产品交易":
          this.$router.push({
            path: "licaiapplyDo",
            query: { applyId: row.applyId, view: "view" }
          });
          break;
        case "债券交易":
          this.$router.push({
            path: "viewBondDetail",
            query: { applyId: row.applyId, view: "view" }
          });
          break;
        case "同业拆借交易":
          this.$router.push({
            path: "viewInterDetail",
            query: { applyId: row.applyId, view: "view" }
          });
          break;
        case "存放同业交易":
          this.$router.push({
            path: "viewNostro",
            query: { applyId: row.applyId, view: "view" }
          });
          break;
        case "债券回购交易":
          this.$router.push({
            path: "viewZqhgDetail",
            query: { applyId: row.applyId, view: "view" }
          });
          break;
        case "同业存单交易":
          this.$router.push({
            path: "viewNcdDetail",
            query: { applyId: row.applyId, view: "view" }
          });
          break;
        case "债券借贷交易":
          this.$router.push({
            path: "viewZqjdDetail",
            query: { applyId: row.applyId, view: "view" }
          });
          break;
        default:
          break;
      }
    },
    morechoose() {
      this.$bus.$emit("chooseBank", {
        callback: banks => {
          let banksName = [];
          let banksId = [];
          banks.forEach((item, index) => {
            banksName.push(item.bName);
            banksId.push(item.bId);
          });
          this.banksArr = banksName;
          this.banksObj = banks;
          this.banks = banksName.join(",");
          this.banksId = banksId.join(",");
        }
      });
    },
    // 分页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getdata(val);
    }
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (
        (from.path == "/licaiapplyDo" ||
          from.path == "/viewBondDetail" ||
          from.path == "/viewInterDetail" ||
          from.path == "/viewNostro" ||
          from.path == "/viewZqhgDetail" ||
          from.path == "/viewNcdDetail" ||
          from.path == "/viewZqjdDetail") &&
        sessionStorage.TransactionRecordParams
      ) {
        vm.paramsGetdata();
      } else {
        vm.getdata(1);
      }
    });
  }
};
</script>

<style lang="scss">
#TransactionRecord {
  .homeTitle {
    &.admin {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 20px;
      border-bottom: 1px solid black;
      .el-input {
        width: 300px;
      }
    }
  }
  .queryCon {
    background: white;
    padding: 0 20px 20px;
    .qSeles {
      padding-top: 20px;
      .xuanze {
        display: flex;
        align-items: center;
        margin-bottom: 10px;
        flex-wrap: wrap;
        .xuanzeCon {
          height: 40px;
          display: flex;
          align-items: center;
          margin-right: 30px;
          .el-input {
            width: 200px;
          }
        }
      }
      .btnAb {
        margin-top: 30px;
        .el-button {
          margin-right: 30px;
        }
      }
    }
  }
  .table {
    background: white;
    .tablecon {
      padding: 20px;
    }
    .pgfy {
      padding: 20px;
      padding-top: 10px;
      text-align: right;
    }
  }
}
</style>
